package net.lovesrc.support.service.mobileverifycode;

import com.alibaba.fastjson.JSONObject;
import com.zhouzhou.fastjava.BizException;
import com.zhouzhou.fastjava.http.Requests;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class YiMaMobileVerifyCodeService extends AbstractMobileVerifyCodeService implements MobileVerifyCodeService {

    private static final Logger logger = LoggerFactory.getLogger(YiMaMobileVerifyCodeService.class);

    public static final String DEFAULT_HOST = "http://api.51ym.me";

    private String username;

    private String password;

    private String gatewayUrl;

    public YiMaMobileVerifyCodeService(String host, String username, String password) {
        this.username = username;
        this.password = password;
        this.gatewayUrl = host + "/UserInterface.aspx";
    }

    @Override
    protected String doGetVerifyCodeFromPhone(String itemId, String phone) {
        JSONObject params = new JSONObject();
        params.put("action", "getsms");
        params.put("token", getToken());
        params.put("mobile", phone);
        params.put("itemid", itemId);
        params.put("release", "1");
        String body = Requests.get(gatewayUrl)
                .params(params)
                .execute()
                .asText();
        if (body.startsWith("success|")) {
            return getSuccessDataBody(body);
        }
        return null;
    }

    @Override
    public void addBlack(String itemId, String phone) {
        logger.info("Add Black Phone: {}", phone);
        Requests.get(gatewayUrl)
                .param("action", "addignore")
                .param("token", getToken())
                .param("mobile", phone)
                .param("itemid", itemId)
                .execute()
                .asText();
    }

    @Override
    public void releasePhone(String itemId, String phone) {
        Requests.get(gatewayUrl)
                .param("action", "release")
                .param("itemid", itemId)
                .param("token", getToken())
                .param("mobile", phone)
                .execute()
                .asText();
    }

    @Override
    public String getPhone(String itemId, String phone) {
       return getPhone(itemId,phone,null);
    }

    @Override
    public String getPhone(String itemId, String phone, String card) {
        String body = Requests.get(gatewayUrl)
                .param("action", "getmobile")
                .param("itemid", itemId)
                .param("token", getToken())
                .param("mobile", phone)
                .execute()
                .asText();
        if (body.startsWith("success|")) {
            return getSuccessDataBody(body);
        } else {
            throw BizException.messageOf("获取手机号失败");
        }
    }

    private String getSuccessDataBody(String body) {
        return body.substring("success|".length());
    }

    @Override
    protected String login() {
        String body = Requests.get(gatewayUrl)
                .param("action", "login")
                .param("username", username)
                .param("password", password)
                .execute()
                .asText();
        if (body.startsWith("success|")) {
            return getSuccessDataBody(body);
        } else {
            throw BizException.messageOf("登录失败");
        }
    }

}
